#encoding:utf-8

'''
data=2017/1/11 下午4:24
__author__='binpo',
author_email='binpocn@163.com',
'''


import datetime
from peewee import *

database = SqliteDatabase('wallet.db')

class BaseModel(Model):
    class Meta:
        database = database

class User(BaseModel):

    username = CharField(unique=True)
    password = CharField()
    email = CharField()
    join_date = DateTimeField()

    class Meta:
        order_by = ('username',)


class Transfer(BaseModel):

    request_ip = CharField(default='')
    amount = CharField(default='')
    edc_account = CharField(default='')
    content = TextField(default='')
    status = BooleanField(default=False)
    session = CharField(default='')
    pub_date = DateTimeField(default=datetime.datetime.now)

    class Meta:
        order_by = ('-pub_date',)

class Exchange(BaseModel):

    echange_type = CharField(max_length=20)          #e-chat  or etao  类型
    amount = CharField(max_length=20)                #edr数量
    app_account = CharField(max_length=20,default='')#memo  e-chat或者etao的账号
    session = CharField(max_length=20,default='')       #session id
    block_num = CharField(max_length=20,unique=True,default='') #block num
    content = TextField(default='')
    status = BooleanField(default=False)
    pub_date = DateTimeField(default=datetime.datetime.now)
    class Meta:
        order_by = ('-pub_date',)


class QueryRecord(BaseModel):

    start = IntegerField(default='')
    limit = IntegerField(default='')
    record_type = CharField(max_length=20,default='e-chat')#e-chat  and etao
    pub_date = DateTimeField(default=datetime.datetime.now)
    class Meta:
        order_by = ('-pub_date',)

class Tasks(BaseModel):

    is_execute = BooleanField()
    back_params = CharField(max_length=1024)
    result = TextField()
    ecexute_date = DateTimeField()
    pub_date = DateTimeField(default=datetime.datetime.now)

    class Meta:
        order_by = ('-pub_date',)

class EchatCoin(BaseModel):
    
    filename =  CharField(max_length=128)
    file_path = CharField(max_length=128)
    title = CharField(max_length=128)
    description = CharField(max_length=128)
    cover = CharField(max_length=1024)
    pub_date = DateTimeField(default=datetime.datetime.now)

    class Meta:
        order_by = ('-pub_date',)
#database.drop_tables([EchatCoin])
#database.create_tables([EchatCoin])
#database.create_tables([EchatCoin,QueryRecord,Exchange,Tasks])
# qr = QueryRecord()
# qr.start=0
# qr.limit=20
# qr.record_type='etao'
# qr.save()
# qr = QueryRecord()
# qr.start=0
# qr.limit=20
# qr.record_type='e-chat'
# qr.save()
# qr = Exchange.select().where(Exchange.block_num==100).first()
# print(qr)
# print qr.__dict__
# exit(0)
# qr.start=100+10
# qr.limit=10
# qr.save()

#
# import datetime
# import json
# data=[{'key':'value'},{'key':'value4-----111111-------------------'}]
# transfer = Transfer()
#
# transfer.request_ip = '127.0.0.1'
# transfer.amount = '0.02'
# transfer.edc_account = 'binpocn163.com'
# transfer.content = '我们都是好同志'
# transfer.result = ''
# transfer.status = 0
# transfer.pub_date = datetime.datetime.now()
# d = transfer.save()
# print d,type(d)
# transfer.status=3
# transfer.content=json.dumps(data)
# transfer.save()
# '''
# '''
# list = Transfer.select().order_by(Transfer.pub_date.desc())#.offset(2).limit(100)
#
# for l in list:
#     print l.id,l.content



# data = {'memo': '\u8fd9\u662f\u4e00\u4e2a\u6d4b\u8bd5\u4fe1\u606f',
#  'description': 'Transfer 0.001 EDC from binpocn163.com to e-chat -- Memo: \u8fd9\u662f\u4e00\u4e2a\u6d4b\u8bd5\u4fe1\u606f   (Fee: 0.001 EDC)',
#  'op': {'op_in_trx': 0,
#         'id': '1.11.6402623',
#         'block_num': 2625327,
#         'virtual_op': 62046,
#         'result': [0, {}],
#         'trx_in_block': 6,
#         'op': [0, {'memo': {'to': 'EDC64XJ3pfFKkEp9r63hbr5S5S6ZeUAVUcJYUm3RSfyVA7kuAraU9',
#                             'from': 'EDC85A1XUwBc6qSP9hSfHf6uusd3rHBkVfUFmXv8qS4YiaQ1bDE3A',
#                             'nonce': '380088287852123',
#                             'message': '51bf32f8c5d84355dac037dd556a9c57c6a9750db1631d139cde99fe42175b17'},
#                    'amount': {'asset_id': '1.3.1', 'amount': 1},
#                    'to': '1.2.328095',
#                    'from': '1.2.321340', 'extensions': [],
#                    'fee': {'asset_id': '1.3.1', 'amount': 1}
#                    }
#                ]
#         }
#  }
# import json
#
# exchange = Exchange()
# exchange.echange_type = ''
# exchange.amount = data.get('op').get('op')[1].get('amount').get('amount')
# exchange.app_account = data.get('memo')
# exchange.session = ''
# exchange.block_num = data.get('op').get('block_num')
# exchange.content = json.dumps(data)
# exchange.save()
